<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能客户关系管理助手 - 用户管理</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4895ef;
            --dark: #1e1e2c;
            --darker: #14141f;
            --light: #f8f9fa;
            --success: #4caf50;
            --warning: #ff9800;
            --danger: #f44336;
            --gray: #6c757d;
            --border-radius: 8px;
            --transition: all 0.3s ease;
            --user-1: #ff6b6b;
            --user-2: #4ecdc4;
            --user-3: #ffd166;
            --user-4: #6a0572;
        }

        body {
            background-color: var(--darker);
            color: var(--light);
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 250px;
            background-color: var(--dark);
            padding: 20px 0;
            height: 100vh;
            position: fixed;
            transition: var(--transition);
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
            z-index: 100;
        }

        .logo-container {
            display: flex;
            align-items: center;
            padding: 0 20px 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .logo {
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, var(--accent), var(--primary));
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 10px;
        }

        .logo i {
            font-size: 20px;
        }

        .logo-text {
            font-size: 20px;
            font-weight: 700;
            background: linear-gradient(to right, var(--accent), var(--primary));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .nav-links {
            list-style: none;
            padding: 0 15px;
        }

        .nav-links li {
            margin-bottom: 5px;
        }

        .nav-links a {
            display: flex;
            align-items: center;
            padding: 12px 15px;
            text-decoration: none;
            color: rgba(255, 255, 255, 0.7);
            border-radius: var(--border-radius);
            transition: var(--transition);
        }

        .nav-links a:hover, .nav-links a.active {
            background-color: rgba(67, 97, 238, 0.2);
            color: var(--light);
        }

        .nav-links a i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }

        /* 主内容区样式 */
        .main-content {
            flex: 1;
            margin-left: 250px;
            padding: 20px;
            transition: var(--transition);
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .page-title {
            font-size: 24px;
            font-weight: 600;
            display: flex;
            align-items: center;
        }

        .page-title i {
            margin-right: 10px;
            color: var(--accent);
        }

        .user-actions {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-id-input {
            display: flex;
            gap: 10px;
            align-items: center;
            background-color: rgba(255, 255, 255, 0.08);
            padding: 8px 15px;
            border-radius: 30px;
        }

        .user-id-input input {
            background: transparent;
            border: none;
            color: white;
            font-size: 14px;
            width: 120px;
        }

        .user-id-input input::placeholder {
            color: rgba(255, 255, 255, 0.6);
        }

        .user-id-input button {
            background: var(--primary);
            border: none;
            color: white;
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 12px;
            cursor: pointer;
        }

        .search-container {
            display: flex;
            flex-direction: column;
        }

        .search-container label {
            font-size: 14px;
            margin-bottom: 5px;
            opacity: 0.8;
        }

        .search-box {
            position: relative;
            height: 40px;
            display: flex;
            align-items: center;
        }

        .search-box input {
            background-color: rgba(255, 255, 255, 0.08);
            border: none;
            padding: 0 15px 0 40px;
            border-radius: 30px;
            color: var(--light);
            width: 250px;
            height: 100%;
            line-height: 1;
            margin: 0;
            transition: var(--transition);
        }

        .search-box input:focus {
            outline: none;
            background-color: rgba(255, 255, 255, 0.15);
            width: 300px;
        }

        .search-box i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: rgba(255, 255, 255, 0.5);
            margin: 0;
        }

        .user-profile {
            display: flex;
            align-items: center;
            background-color: rgba(255, 255, 255, 0.08);
            padding: 8px 15px;
            border-radius: 30px;
            cursor: pointer;
            transition: var(--transition);
        }

        .user-profile:hover {
            background-color: rgba(255, 255, 255, 0.15);
        }

        .user-profile img {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            margin-right: 10px;
            object-fit: cover;
        }

        /* 卡片样式 */
        .card {
            background-color: var(--dark);
            border-radius: var(--border-radius);
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
            margin-bottom: 25px;
            overflow: hidden;
        }

        .card-header {
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .card-title {
            font-size: 18px;
            font-weight: 600;
        }

        .card-body {
            padding: 20px;
        }

        /* 用户管理区域 */
        .user-management {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .action-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }

        .filters {
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
            margin-bottom: 20px;
            background-color: rgba(255, 255, 255, 0.05);
            padding: 15px;
            border-radius: var(--border-radius);
        }

        .filter-group {
            display: flex;
            flex-direction: column;
            min-width: 200px;
        }

        .filter-group label {
            font-size: 14px;
            margin-bottom: 5px;
            opacity: 0.8;
        }

        .filter-group select, .filter-group input {
            background-color: rgba(255, 255, 255, 0.08);
            border: none;
            padding: 10px 15px;
            border-radius: var(--border-radius);
            color: var(--light);
        }

        .filter-group select:focus, .filter-group input:focus {
            outline: none;
            background-color: rgba(255, 255, 255, 0.12);
        }

        /* 用户卡片网格 */
        .users-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 20px;
            margin-bottom: 25px;
        }

        .user-card {
            background: var(--dark);
            border-radius: var(--border-radius);
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            transition: var(--transition);
            display: flex;
            flex-direction: column;
            height: 100%;
        }

        .user-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(67, 97, 238, 0.3);
        }

        .user-avatar {
            height: 180px;
            background-size: cover;
            background-position: center;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .avatar-circle {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            background-size: cover;
            background-position: center;
            border: 3px solid rgba(255, 255, 255, 0.1);
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
        }

        .user-badge {
            position: absolute;
            top: 10px;
            right: 10px;
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
            background-color: rgba(76, 175, 80, 0.2);
            color: var(--success);
        }

        .user-info {
            padding: 20px;
            flex: 1;
            display: flex;
            flex-direction: column;
        }

        .user-header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
        }

        .user-name {
            font-size: 18px;
            font-weight: 600;
            color: var(--light);
        }

        .user-role {
            font-size: 14px;
            color: var(--accent);
            background: rgba(67, 97, 238, 0.1);
            padding: 3px 8px;
            border-radius: 20px;
        }

        .user-details {
            margin-bottom: 15px;
            color: rgba(255, 255, 255, 0.7);
            font-size: 14px;
            flex: 1;
        }

        .user-detail {
            display: flex;
            margin-bottom: 8px;
            align-items: center;
        }

        .user-detail i {
            margin-right: 8px;
            width: 20px;
            color: var(--accent);
        }

        .user-status {
            display: flex;
            align-items: center;
            font-size: 14px;
            margin-top: 10px;
        }

        .status-indicator {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin-right: 5px;
        }

        .status-active {
            background-color: var(--success);
        }

        .status-inactive {
            background-color: var(--danger);
        }

        .user-actions {
            display: flex;
            justify-content: flex-end;
            padding: 15px;
            background: rgba(255, 255, 255, 0.05);
            gap: 10px;
        }

        /* 表格样式 */
        .table-container {
            overflow-x: auto;
            border-radius: var(--border-radius);
            background-color: rgba(255, 255, 255, 0.05);
        }

        table {
            width: 100%;
            border-collapse: collapse;
            min-width: 1000px;
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        th {
            background-color: rgba(67, 97, 238, 0.2);
            font-weight: 600;
            color: var(--accent);
        }

        tr:hover {
            background-color: rgba(255, 255, 255, 0.05);
        }

        .status-badge {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
        }

        .status-active {
            background-color: rgba(76, 175, 80, 0.2);
            color: var(--success);
        }

        .status-inactive {
            background-color: rgba(244, 67, 54, 0.2);
            color: var(--danger);
        }

        .actions {
            display: flex;
            gap: 8px;
        }

        .action-btn {
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            border: none;
            background-color: rgba(255, 255, 255, 0.1);
            color: var(--light);
            cursor: pointer;
            transition: var(--transition);
        }

        .action-btn:hover {
            background-color: var(--accent);
        }

        .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 10px 20px;
            border-radius: var(--border-radius);
            border: none;
            cursor: pointer;
            font-weight: 500;
            transition: var(--transition);
        }

        .btn i {
            margin-right: 8px;
        }

        .btn-primary {
            background-color: var(--primary);
            color: white;
        }

        .btn-primary:hover {
            background-color: var(--secondary);
        }

        .btn-success {
            background-color: var(--success);
            color: white;
        }

        .btn-warning {
            background-color: var(--warning);
            color: white;
        }

        .btn-danger {
            background-color: var(--danger);
            color: white;
        }

        .btn-outline {
            background-color: transparent;
            border: 1px solid rgba(255, 255, 255, 0.2);
            color: rgba(255, 255, 255, 0.7);
        }

        .btn-outline:hover {
            background-color: rgba(255, 255, 255, 0.1);
            color: var(--light);
        }

        .loading {
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .spinner {
            width: 24px;
            height: 24px;
            border: 3px solid rgba(255, 255, 255, 0.3);
            border-radius: 50%;
            border-top-color: var(--accent);
            animation: spin 1s linear infinite;
            margin-right: 10px;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        /* 模态框样式 */
        .modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.7);
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 1000;
        }

        .modal {
            background-color: var(--dark);
            width: 90%;
            max-width: 800px;
            border-radius: var(--border-radius);
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            overflow: hidden;
        }

        .modal-header {
            padding: 20px;
            background-color: rgba(67, 97, 238, 0.2);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .modal-title {
            font-size: 18px;
            font-weight: 600;
            color: var(--accent);
        }

        .modal-close {
            background: none;
            border: none;
            color: var(--light);
            font-size: 20px;
            cursor: pointer;
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            transition: var(--transition);
        }

        .modal-close:hover {
            background-color: rgba(255, 255, 255, 0.1);
        }

        .modal-body {
            padding: 20px;
            max-height: 70vh;
            overflow-y: auto;
        }

        .form-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            opacity: 0.9;
        }

        .form-group input, .form-group select, .form-group textarea {
            width: 100%;
            padding: 12px 15px;
            background-color: rgba(255, 255, 255, 0.08);
            border: none;
            border-radius: var(--border-radius);
            color: var(--light);
        }

        .form-group textarea {
            height: 100px;
            resize: vertical;
        }

        .form-group input:focus, .form-group select:focus, .form-group textarea:focus {
            outline: none;
            background-color: rgba(255, 255, 255, 0.12);
        }

        .modal-footer {
            padding: 15px 20px;
            background-color: rgba(255, 255, 255, 0.05);
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }

        /* 统计卡片样式 */
        .stats-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 25px;
        }

        .stat-card {
            background: var(--dark);
            border-radius: var(--border-radius);
            padding: 20px;
            display: flex;
            flex-direction: column;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
        }

        .stat-value {
            font-size: 28px;
            font-weight: 700;
            margin-bottom: 10px;
        }

        .stat-label {
            font-size: 14px;
            color: rgba(255, 255, 255, 0.7);
        }

        .stat-icon {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 15px;
            font-size: 20px;
        }

        .stat-icon.users {
            background: rgba(67, 97, 238, 0.2);
            color: var(--accent);
        }

        .stat-icon.admins {
            background: rgba(255, 107, 107, 0.2);
            color: var(--user-1);
        }

        .stat-icon.active {
            background: rgba(76, 175, 80, 0.2);
            color: var(--success);
        }

        .stat-icon.inactive {
            background: rgba(255, 152, 0, 0.2);
            color: var(--warning);
        }

        /* 响应式设计 */
        @media (max-width: 992px) {
            .sidebar {
                width: 80px;
            }

            .logo-text, .link-text {
                display: none;
            }

            .nav-links a {
                justify-content: center;
            }

            .nav-links a i {
                margin-right: 0;
                font-size: 20px;
            }

            .main-content {
                margin-left: 80px;
            }

            .filters {
                flex-direction: column;
            }
        }

        @media (max-width: 768px) {
            .search-box input {
                width: 150px;
            }

            .search-box input:focus {
                width: 180px;
            }

            .header {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }

            .user-actions {
                width: 100%;
                justify-content: space-between;
            }

            .action-bar {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }

            .users-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
<!-- 侧边导航 -->
<div class="sidebar">
    <div class="logo-container">
        <div class="logo">
            <i class="fas fa-brain"></i>
        </div>
        <div class="logo-text">智能CRM</div>
    </div>

    <ul class="nav-links">
        <li><a href="/main"><i class="fas fa-home"></i> <span class="link-text">AI助手</span></a></li>
        <li><a href="/customer"><i class="fas fa-users"></i> <span class="link-text">客户管理</span></a></li>
        <li><a href="/base_dict"><i class="fas fa-book"></i> <span class="link-text">数据字典</span></a></li>
        <li><a href="/usermanage" class="active"><i class="fas fa-user-cog"></i> <span class="link-text">用户管理</span></a></li>
        <li><a href="/product"><i class="fas fa-box"></i> <span class="link-text">产品管理</span></a></li>
        <li><a href="/orderitems"><i class="fas fa-shopping-cart"></i> <span class="link-text">订单管理</span></a></li>
    </ul>
</div>

<!-- 主内容区 -->
<div class="main-content" id="app">
    <!-- 头部区域 -->
    <div class="header">
        <div class="page-title">
            <i class="fas fa-user-cog"></i> 用户管理
        </div>

            <div class="user-profile">
                <img src="https://randomuser.me/api/portraits/men/32.jpg" alt="用户头像">
                <span th:text="${session.username}"></span>
            </div>
    </div>

    <!-- 用户管理区域 -->
    <div class="user-management">
        <!-- 统计卡片 -->
        <div class="stats-container">
            <div class="stat-card">
                <div class="stat-icon users">
                    <i class="fas fa-users"></i>
                </div>
                <div class="stat-value">{{ users.length }}</div>
                <div class="stat-label">用户总数</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon admins">
                    <i class="fas fa-crown"></i>
                </div>
                <div class="stat-value">{{ adminUsers }}</div>
                <div class="stat-label">管理员</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon active">
                    <i class="fas fa-user-check"></i>
                </div>
                <div class="stat-value">{{ activeUsers }}</div>
                <div class="stat-label">活跃用户</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon inactive">
                    <i class="fas fa-user-clock"></i>
                </div>
                <div class="stat-value">{{ inactiveUsers }}</div>
                <div class="stat-label">未激活用户</div>
            </div>
        </div>

        <div class="action-bar">
            <h2><i class="fas fa-user-friends"></i> 用户列表</h2>
            <div>
                <button class="btn btn-outline" @click="viewMode = 'grid'" :class="{'btn-primary': viewMode === 'grid'}">
                    <i class="fas fa-th"></i> 网格视图
                </button>
                <button class="btn btn-outline" @click="viewMode = 'table'" :class="{'btn-primary': viewMode === 'table'}">
                    <i class="fas fa-table"></i> 表格视图
                </button>
                <button class="btn btn-primary" @click="openAddModal">
                    <i class="fas fa-plus"></i> 添加新用户
                </button>
            </div>
        </div>

        <!-- 筛选区域 -->
        <div class="filters">
            <div class="filter-group">
                <label>用户角色</label>
                <select v-model="filters.role" @change="applyFilters">
                    <option value="">全部角色</option>
                    <option v-for="role in roles" :value="role">{{ role }}</option>
                </select>
            </div>

            <div class="filter-group">
                <label>用户状态</label>
                <select v-model="filters.status" @change="applyFilters">
                    <option value="">全部状态</option>
                    <option value="active">活跃</option>
                    <option value="inactive">未激活</option>
                    <option value="suspended">已禁用</option>
                </select>
            </div>

            <div class="filter-group">
                <label>注册时间</label>
                <select v-model="filters.registration" @change="applyFilters">
                    <option value="">全部时间</option>
                    <option value="today">今天</option>
                    <option value="week">本周</option>
                    <option value="month">本月</option>
                </select>
            </div>

            <div class="filter-group">
                <label>部门</label>
                <select v-model="filters.department" @change="applyFilters">
                    <option value="">全部部门</option>
                    <option v-for="dept in departments" :value="dept">{{ dept }}</option>
                </select>
            </div>
            <div class="search-container">
                <label>搜索：</label>
                <div class="search-box">
                    <i class="fas fa-search"></i>
                    <input type="text" v-model="searchQuery" placeholder="搜索用户..." @input="searchCustomers">
                </div>
            </div>
        </div>

        <!-- 用户网格视图 -->
        <div class="users-grid" v-if="viewMode === 'grid'">
            <div class="user-card" v-for="user in paginatedUsers" :key="user.id">
                <div class="user-avatar" :style="{ backgroundColor: user.color }">
                    <div class="avatar-circle" :style="{ backgroundImage: 'url(' + user.avatar + ')' }"></div>
                    <span class="user-badge" v-if="user.status === 'active'">活跃</span>
                </div>
                <div class="user-info">
                    <div class="user-header">
                        <div class="user-name">{{ user.name }}</div>
                        <div class="user-role">{{ user.role }}</div>
                    </div>
                    <div class="user-details">
                        <div class="user-detail">
                            <i class="fas fa-envelope"></i>
                            <span>{{ user.email }}</span>
                        </div>
                        <div class="user-detail">
                            <i class="fas fa-phone"></i>
                            <span>{{ user.phone || '未提供' }}</span>
                        </div>
                        <div class="user-detail">
                            <i class="fas fa-building"></i>
                            <span>{{ user.department }}</span>
                        </div>
                        <div class="user-status">
                            <div class="status-indicator" :class="{
                                'status-active': user.status === 'active',
                                'status-inactive': user.status !== 'active'
                            }"></div>
                            {{ getStatusText(user.status) }}
                        </div>
                    </div>
                </div>
                <div class="user-actions">
                    <button class="action-btn" @click="openEditModal(user)">
                        <i class="fas fa-edit"></i>
                    </button>
                    <button class="action-btn" @click="toggleUserStatus(user)">
                        <i class="fas" :class="{'fa-toggle-on': user.status === 'active', 'fa-toggle-off': user.status !== 'active'}"></i>
                    </button>
                    <button class="action-btn" @click="deleteUser(user.id)">
                        <i class="fas fa-trash"></i>
                    </button>
                </div>
            </div>
        </div>

        <!-- 用户表格视图 -->
        <div class="card" v-if="viewMode === 'table'">
            <div class="table-container">
                <table>
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户信息</th>
                        <th>角色</th>
                        <th>部门</th>
                        <th>邮箱</th>
                        <th>状态</th>
                        <th>注册时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-if="loading">
                        <td colspan="8" class="loading">
                            <div class="spinner"></div>
                            <span>加载用户数据中...</span>
                        </td>
                    </tr>
                    <tr v-else-if="filteredUsers.length === 0">
                        <td colspan="8" style="text-align: center; padding: 30px;">
                            没有找到匹配的用户
                        </td>
                    </tr>
                    <tr v-for="user in paginatedUsers" :key="user.id">
                        <td>{{ user.id }}</td>
                        <td>
                            <div style="display: flex; align-items: center; gap: 10px;">
                                <div style="width: 40px; height: 40px; border-radius: 50%; background-size: cover; background-position: center;"
                                     :style="{ backgroundImage: 'url(' + user.avatar + ')' }"></div>
                                <div>
                                    <div style="font-weight: 600;">{{ user.name }}</div>
                                    <div style="font-size: 13px; color: #aaa;">{{ user.phone || '未提供' }}</div>
                                </div>
                            </div>
                        </td>
                        <td>{{ user.role }}</td>
                        <td>{{ user.department }}</td>
                        <td>{{ user.email }}</td>
                        <td>
                                <span class="status-badge" :class="{
                                    'status-active': user.status === 'active',
                                    'status-inactive': user.status === 'inactive',
                                    'status-warning': user.status === 'suspended'
                                }">
                                    {{ getStatusText(user.status) }}
                                </span>
                        </td>
                        <td>{{ formatDate(user.created_at) }}</td>
                        <td>
                            <div class="actions">
                                <button class="action-btn" @click="openEditModal(user)">
                                    <i class="fas fa-edit"></i>
                                </button>
                                <button class="action-btn" @click="toggleUserStatus(user)">
                                    <i class="fas" :class="{'fa-toggle-on': user.status === 'active', 'fa-toggle-off': user.status !== 'active'}"></i>
                                </button>
                                <button class="action-btn" @click="deleteUser(user.id)">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- 分页控件 -->
        <div class="pagination" v-if="filteredUsers.length > 0" style="display: flex; justify-content: center; align-items: center; margin-top: 20px;">
            <button class="btn btn-outline" :disabled="currentPage === 1" @click="prevPage">
                <i class="fas fa-chevron-left"></i> 上一页
            </button>
            <span style="margin: 0 15px;">第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
            <button class="btn btn-outline" :disabled="currentPage === totalPages" @click="nextPage">
                下一页 <i class="fas fa-chevron-right"></i>
            </button>
        </div>
    </div>

    <!-- 添加/编辑用户模态框 -->
    <div class="modal-overlay" v-if="showModal">
        <div class="modal">
            <div class="modal-header">
                <div class="modal-title">
                    <i class="fas" :class="{'fa-user-plus': isAdding, 'fa-user-edit': !isAdding}"></i>
                    {{ isAdding ? '添加新用户' : '编辑用户信息' }}
                </div>
                <button class="modal-close" @click="closeModal">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            <div class="modal-body">
                <div class="form-grid">
                    <div class="form-group">
                        <label>用户名 *</label>
                        <input type="text" v-model="currentUser.name" placeholder="输入用户名">
                    </div>
                    <div class="form-group">
                        <label>用户角色 *</label>
                        <select v-model="currentUser.role">
                            <option v-for="role in roles" :value="role">{{ role }}</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>邮箱地址 *</label>
                        <input type="email" v-model="currentUser.email" placeholder="输入邮箱地址">
                    </div>
                    <div class="form-group">
                        <label>手机号码</label>
                        <input type="tel" v-model="currentUser.phone" placeholder="输入手机号码">
                    </div>
                    <div class="form-group">
                        <label>部门</label>
                        <select v-model="currentUser.department">
                            <option v-for="dept in departments" :value="dept">{{ dept }}</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>用户状态</label>
                        <select v-model="currentUser.status">
                            <option value="active">活跃</option>
                            <option value="inactive">未激活</option>
                            <option value="suspended">已禁用</option>
                        </select>
                    </div>
                    <div class="form-group" v-if="isAdding">
                        <label>初始密码 *</label>
                        <input type="password" v-model="currentUser.password" placeholder="设置初始密码">
                    </div>
                    <div class="form-group" style="grid-column: 1 / -1;">
                        <label>备注信息</label>
                        <textarea v-model="currentUser.notes" placeholder="输入用户备注信息"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-outline" @click="closeModal">取消</button>
                <button class="btn btn-primary" @click="saveUser">
                    <i class="fas fa-save"></i> {{ isAdding ? '添加用户' : '保存更改' }}
                </button>
            </div>
        </div>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            userId: 'admin_' + Math.floor(Math.random() * 1000),
            searchQuery: '',
            users: [],
            filteredUsers: [],
            loading: true,
            showModal: false,
            isAdding: true,
            currentUser: null,
            currentPage: 1,
            itemsPerPage: 8,
            viewMode: 'grid', // 'grid' or 'table'

            // 筛选条件
            filters: {
                role: '',
                status: '',
                registration: '',
                department: ''
            },

            // 用户数据字典
            roles: ['系统管理员', '销售经理', '销售代表', '客服专员', '产品经理', '数据分析师'],
            departments: ['销售部', '市场部', '技术部', '客服部', '产品部', '人力资源部'],
            statusColors: {
                'active': '#4caf50',
                'inactive': '#ff9800',
                'suspended': '#f44336'
            }
        },
        computed: {
            adminUsers() {
                return this.users.filter(u => u.role === '系统管理员').length;
            },
            activeUsers() {
                return this.users.filter(u => u.status === 'active').length;
            },
            inactiveUsers() {
                return this.users.filter(u => u.status !== 'active').length;
            },
            totalPages() {
                return Math.ceil(this.filteredUsers.length / this.itemsPerPage);
            },
            paginatedUsers() {
                const start = (this.currentPage - 1) * this.itemsPerPage;
                const end = start + this.itemsPerPage;
                return this.filteredUsers.slice(start, end);
            }
        },
        mounted() {
            this.currentUser = this.getEmptyUser();
            this.fetchUsers();
        },
        methods: {
            setUserId() {
                if (!this.userId) {
                    alert('请输入用户ID');
                    return;
                }
                this.fetchUsers();
            },

            // 获取用户数据
            fetchUsers() {
                this.loading = true;
                axios.get('/user/all')
                    .then(response => {
                        if (response.data && response.data.success) {
                            this.users = response.data.data.map(user => {
                                return {
                                    id: user.id,
                                    name: user.name,
                                    role: user.role,
                                    email: user.email,
                                    phone: user.phone,
                                    department: user.department,
                                    status: user.status,
                                    avatar: user.avatar || ('https://randomuser.me/api/portraits/men/' + Math.floor(Math.random() * 50) + '.jpg'),

                                    color: this.getRandomColor(),
                                    created_at: new Date(user.createdAt),
                                    notes: user.notes || ''
                                };
                            });
                            this.filteredUsers = [...this.users];
                        } else {
                            console.error('获取用户列表失败:', response.data.message);
                        }
                        this.loading = false;
                    })
                    .catch(error => {
                        console.error('获取用户列表失败:', error);
                        this.loading = false;
                    });
            },

            // 搜索用户
            searchUsers() {
                if (!this.searchQuery) {
                    this.filteredUsers = [...this.users];
                    return;
                }

                const query = this.searchQuery.toLowerCase();
                this.filteredUsers = this.users.filter(user =>
                    user.name.toLowerCase().includes(query) ||
                    user.email.toLowerCase().includes(query) ||
                    user.role.toLowerCase().includes(query) ||
                    (user.phone && user.phone.includes(query))
                );
            },

            // 应用筛选
            applyFilters() {
                this.filteredUsers = this.users.filter(user => {
                    // 角色筛选
                    if (this.filters.role && user.role !== this.filters.role) {
                        return false;
                    }

                    // 状态筛选
                    if (this.filters.status && user.status !== this.filters.status) {
                        return false;
                    }

                    // 注册时间筛选
                    if (this.filters.registration) {
                        const now = new Date();
                        const userDate = new Date(user.created_at);

                        if (this.filters.registration === 'today') {
                            return userDate.toDateString() === now.toDateString();
                        }
                        if (this.filters.registration === 'week') {
                            const oneWeekAgo = new Date(now);
                            oneWeekAgo.setDate(now.getDate() - 7);
                            return userDate >= oneWeekAgo;
                        }
                        if (this.filters.registration === 'month') {
                            const oneMonthAgo = new Date(now);
                            oneMonthAgo.setMonth(now.getMonth() - 1);
                            return userDate >= oneMonthAgo;
                        }
                    }

                    // 部门筛选
                    if (this.filters.department && user.department !== this.filters.department) {
                        return false;
                    }

                    return true;
                });

                this.currentPage = 1;
            },

            // 打开添加模态框
            openAddModal() {
                this.currentUser = this.getEmptyUser();
                this.isAdding = true;
                this.showModal = true;
            },

            // 打开编辑模态框
            openEditModal(user) {
                this.currentUser = {...user};
                this.isAdding = false;
                this.showModal = true;
            },

            // 关闭模态框
            closeModal() {
                this.showModal = false;
            },

            // 保存用户
            saveUser() {
                // 验证必填字段
                if (!this.currentUser.name) {
                    alert('用户名不能为空');
                    return;
                }
                if (!this.currentUser.role) {
                    alert('请选择用户角色');
                    return;
                }
                if (!this.currentUser.email) {
                    alert('请输入邮箱地址');
                    return;
                }
                if (this.isAdding && !this.currentUser.password) {
                    alert('请设置初始密码');
                    return;
                }

                const userData = {
                    name: this.currentUser.name,
                    role: this.currentUser.role,
                    email: this.currentUser.email,
                    phone: this.currentUser.phone,
                    department: this.currentUser.department,
                    status: this.currentUser.status,
                    notes: this.currentUser.notes
                };

                if (this.isAdding) {
                    userData.password = this.currentUser.password;
                    axios.post('/user/add', userData)
                        .then(response => {
                            if (response.data && response.data.success) {
                                this.fetchUsers();
                                this.showModal = false;
                                alert(`用户 ${this.currentUser.name} 添加成功！`);
                            } else {
                                alert(`添加用户失败: ${response.data.message}`);
                            }
                        })
                        .catch(error => {
                            alert(`添加用户失败: ${error.response.data.message || error.message}`);
                        });
                } else {
                    userData.id = this.currentUser.id;
                    axios.put('/user/update', userData)
                        .then(response => {
                            if (response.data && response.data.success) {
                                this.fetchUsers();
                                this.showModal = false;
                                alert(`用户 ${this.currentUser.name} 更新成功！`);
                            } else {
                                alert(`更新用户失败: ${response.data.message}`);
                            }
                        })
                        .catch(error => {
                            alert(`更新用户失败: ${error.response.data.message || error.message}`);
                        });
                }
            },

            // 删除用户
            deleteUser(id) {
                if (!confirm('确定要删除此用户吗？此操作不可撤销。')) return;

                axios.delete(`/user/delete/${id}`)
                    .then(response => {
                        if (response.data && response.data.success) {
                            this.fetchUsers();
                            alert('用户删除成功！');
                        } else {
                            alert(`删除用户失败: ${response.data.message}`);
                        }
                    })
                    .catch(error => {
                        alert(`删除用户失败: ${error.response.data.message || error.message}`);
                    });
            },

            // 切换用户状态
            toggleUserStatus(user) {
                const newStatus = user.status === 'active' ? 'suspended' : 'active';

                axios.put(`/user/status/${user.id}?status=${newStatus}`)
                    .then(response => {
                        if (response.data && response.data.success) {
                            user.status = newStatus;
                            alert(`用户 ${user.name} 已${user.status === 'active' ? '激活' : '禁用'}！`);
                        } else {
                            alert(`状态更新失败: ${response.data.message}`);
                        }
                    })
                    .catch(error => {
                        alert(`状态更新失败: ${error.response.data.message || error.message}`);
                    });
            },

            // 获取空用户对象
            getEmptyUser() {
                return {
                    id: 0,
                    name: '',
                    role: '销售代表',
                    email: '',
                    phone: '',
                    department: '销售部',
                    status: 'active',
                    avatar: 'https://cdn-icons-png.flaticon.com/512/3135/3135715.png',
                    color: '#4361ee',
                    created_at: new Date(),
                    password: '',
                    notes: ''
                };
            },

            // 分页方法
            nextPage() {
                if (this.currentPage < this.totalPages) {
                    this.currentPage++;
                }
            },

            prevPage() {
                if (this.currentPage > 1) {
                    this.currentPage--;
                }
            },

            // 格式化日期
            formatDate(date) {
                return new Date(date).toLocaleDateString('zh-CN');
            },

            // 获取状态文本
            getStatusText(status) {
                const statusMap = {
                    'active': '活跃',
                    'inactive': '未激活',
                    'suspended': '已禁用'
                };
                return statusMap[status] || status;
            },

            // 生成随机颜色
            getRandomColor() {
                const colors = [
                    '#4361ee', '#3f37c9', '#4895ef', '#4cc9f0',
                    '#f72585', '#7209b7', '#3a0ca3', '#4ecdc4'
                ];
                return colors[Math.floor(Math.random() * colors.length)];
            },
        }
    });
</script>
</body>
</html>